<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--360浏览器H5兼容设置-->
    <meta name="renderer" content="webkit"/>
    <title>电脑商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--导入核心文件-->
    <script src="../bootstrap3/js/holder.js"></script>
    <link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="../bootstrap3/jquery-1.9.1.min.js"></script>
    <script src="../bootstrap3/js/bootstrap.js"></script>
    <!-- 字体图标 -->
    <link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css"/>
    <link rel="stylesheet" type="text/css" href="../css/top.css"/>
    <link rel="stylesheet" type="text/css" href="../css/footer.css"/>
    <link rel="stylesheet" type="text/css" href="../css/product.css"/>
    <link rel="stylesheet" type="text/css" href="../css/imgmove.css"/>
    <script src="../js/product.js" type="text/javascript" charset="utf-8"></script>
    <script src="../js/imgmove.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
<!--头部-->
<header class="header">
    <!--电脑商城logo-->
    <div class="row">
        <div class="col-md-3">
            <a href="index_user.html">
                <img src="../images/index/stumalllogo.png"/>
            </a>
        </div>
        <!--快捷选项-->
        <div class="col-md-9 top-item">
            <ul class="list-inline pull-right">
                <li><a href="orders_user.html"><span class="fa fa-file-text"></span>&nbsp;订单</a></li>
                <li class="li-split">|</li>
                <li><a href="cart_user.html"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
                <li class="li-split">|</li>
                <li>
                    <!--下列列表按钮 ：管理-->
                    <div class="btn-group">
                        <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">
                            <span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span
                                    class="caret"></span></span>
                        </button>
                        <ul class="dropdown-menu top-dropdown-ul" role="menu">
                            <li><a href="password.html">修改密码</a></li>
                            <li><a href="userdata.html">个人资料</a></li>
                            <li><a href="upload.html">上传头像</a></li>
                            <li><a href="address.html">收货管理 </a></li>
                        </ul>
                    </div>
                </li>
                <li class="li-split">|</li>
                <li><a href="login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
            </ul>
        </div>
    </div>
</header>
<!--导航 -->
<!--分割导航和顶部-->
<div class="row top-nav">
    <div class="col-md-6">
        <ul class="nav nav-pills">
            <li>
                <a href="#"></a>
            </li>
            <li class="active"><a href="index_user.html"><span class="fa fa-home"></span></a></li>
            <div class="col-md-6">
                <form id="form-search" action="search.html" class="form-inline pull-right" role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" id="search" name="title" placeholder="请输入商品名称进行搜索">
                    </div>
                    <input id="btn-search" type="submit" class="btn btn-primary" value="搜索"/>
                </form>
            </div>
        </ul>
    </div>
</div>
<!--头部结束-->
<!--导航结束-->
<!--商品信息展示开始-->
<div class="container">
    <div class="col-md-offset-1 col-md-10">
        <div class="col-md-6">
            <div data="1" class="img-big move-img col-md-12">
                <img id="product-image-1-big" src="../images/portal/11DELLran7000R1605Ssilvery/1_big.png"
                     class="img-responsive"/>
            </div>
            <div data="2" class="img-big move-img col-md-12">
                <img id="product-image-2-big" src="../images/portal/11DELLran7000R1605Ssilvery/2_big.png"
                     class="img-responsive"/>
            </div>
            <div data="3" class="img-big move-img col-md-12">
                <img id="product-image-3-big" src="../images/portal/11DELLran7000R1605Ssilvery/3_big.png"
                     class="img-responsive"/>
            </div>
            <div data="4" class="img-big move-img col-md-12">
                <img id="product-image-4-big" src="../images/portal/11DELLran7000R1605Ssilvery/4_big.png"
                     class="img-responsive"/>
            </div>
            <div data="5" class="img-big move-img col-md-12">
                <img id="product-image-5-big" src="../images/portal/11DELLran7000R1605Ssilvery/5_big.png"
                     class="img-responsive"/>
            </div>
            <br>
            <table class="col-md-10" style="width: max-content;height: 50px">
                <tr>
                    <td data="1" class="col-md-2 img-small">
                        <img id="product-image-1"
                             src="../images/portal/11DELLran7000R1605Ssilvery/1.jpg"
                             class="img-responsive"/>
                    </td>
                    <td data="2" class="col-md-2 img-small">
                        <img id="product-image-2"
                             src="../images/portal/11DELLran7000R1605Ssilvery/2.jpg"
                             class="img-responsive"/>
                    </td>
                    <td data="3" class="col-md-2 img-small">
                        <img id="product-image-3"
                             src="../images/portal/11DELLran7000R1605Ssilvery/3.jpg"
                             class="img-responsive"/>
                    </td>
                    <td data="4" class="col-md-2 img-small">
                        <img id="product-image-4"
                             src="../images/portal/11DELLran7000R1605Ssilvery/4.jpg"
                             class="img-responsive"/>
                    </td>
                    <td data="5" class="col-md-2 img-small">
                        <img id="product-image-5"
                             src="../images/portal/11DELLran7000R1605Ssilvery/5.jpg"
                             class="img-responsive"/>
                    </td>
            </tr>
            </table>
        </div>
        <div class="col-md-6">
            <h3 id="product-title" class="col-md-12 title-row-1">戴尔Dell 燃700R1605银色</h3>
            <p id="product-sell-point" class="col-md-12 text-row-1"><small>仅上海，广州，沈阳仓有货！预购从速！</small></p>
            <div class="col-md-12 price-bar">
                <small>售价：</small> ¥<span id="product-price">4899.00</span>
                <div><small>*退货补运费 *7天无理由退货 *24小时快速退款 </small></div>
                <div>
                    <small>库存：</small> <small id="product-num">100</small>
                    <small>历史售量：</small> <small id="product-priority">100</small>
                </div>
                <div>
                    <small>购买人数：</small> <small id="product-sell_person">100</small>
                    <small>评价分数：</small> <small id="product-judge_mark">10</small>
                </div>
            </div>
            <form class="form-inline product-form col-md-12" role="form">
                <div class="col-md-12 form-space">
                    <label for="num">数量：</label>
                    <input id="numDown" type="button" value="-" class="num-btn"/>
                    <input id="num" type="text" size="2" readonly="readonly" class="num-text" value="1">
                    <input id="numUp" class="num-btn" type="button" value="+"/>
                </div>
                <div class="col-md-12 form-space">
                    <p><small><b>北信科二手商城</b>发货并提供售后服务,当日下单,当日送达</small></p>
                </div>
                <div class="col-md-12 form-space">
                    <input class="btn btn-primary btn-lg btn-block" type="button" value="立即购买">
                </div>
                <div class="col-md-12 form-space">
                    <button id="btn-add-to-cart" type="button" class="btn btn-default go-cart"><span
                            class="fa fa-cart-plus"></span> 加入购物车
                    </button>
                </div>
            </form>
        </div>
    </div>
    <!--商品信息展示结束-->
    <!--评价按钮与商品历史评价-->

	<div class="col-md-offset-1 col-md-10">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h2 class="panel-title">商品评价</h2>
			</div>
			<form id="form-add-one-comment" class="form-horizontal" role="form">
				<input id="uid" name="uid" type="text" class="hide form-control" placeholder="用户uid">
				<input id="bid" name="bid" type="text" class="hide form-control" placeholder="商家bid">
				<input id="oid" name="oid" type="text" class="hide form-control" placeholder="订单oid">
				<input id="pid" name="pid" type="text" class="hide form-control" placeholder="商品pid">

				<div class="form-group">
					<label class="col-md-2 control-label">商品评价：</label>
					<div class="col-md-8">
						<input name="PComment" type="text" class="form-control" placeholder="请输入对商品的评价">
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">商家评价：</label>
					<div class="col-md-8">
						<input name="BComment" type="text" class="form-control" placeholder="请输入对商家的评价">
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">评价分数（默认10分）：</label>
					<div class="col-md-8">
						<input id="judgeMark" name="judgeMark" type="text" class="form-control" placeholder="请输入评价分数（1-10）">
					</div>
				</div>
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						<input id="btn-add-one-comment" type="button" class="btn btn-primary" value="评价" />
					</div>
				</div>
			</form>
		</div>
	</div>
    <div class="col-md-offset-1 col-md-10">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h2 class="panel-title">商品历史评价</h2>
            </div>
            <form role="form">
                <table class="cart-table" width="100%">
                    <thead>
                    <tr>
                        <th width="5%">用户id</th>
                        <th width="5%">商家id</th>
                        <th width="5%">订单id</th>
                        <th width="10%">商品id</th>
                        <th width="20%">商品评价</th>
                        <th width="20%">商家评价</th>
                        <th width="20%">评价分数</th>
                        <th width="20%">商家回复</th>
                    </tr>
                    </thead>
                    <tbody id="comment-list" class="cart-body">
                    <tr>
                        <td>50</td>
                        <td>50</td>
                        <td>50</td>
                        <td>10000005</td>
                        <td>不错不错不错不错不错不错</td>
                        <td>不错不错不错不错不错不错</td>
                        <td>10</td>
                        <td>谢谢谢谢谢谢谢谢</td>
                    </tr>
                    </tbody>
                </table>
                <div class="total-bar">
                </div>
                <label class="col-md-2 control-label"></label>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    let judgeMark=document.querySelector("#judgeMark");
    let id = localStorage.getItem("pid");
    console.log("id=" + id);
    //showProductCommentList();
    $(document).ready(function () {
    	$("#uid").val(localStorage.getItem("uid"));
		$("#bid").val(localStorage.getItem("bid"));
		$("#oid").val(localStorage.getItem("oid"));
		$("#pid").val(localStorage.getItem("pid"));
		console.log($("#form-add-one-comment").serialize());
		$.ajax({
            url: "/product/" + id + "/details",
            type: "GET",
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    console.log("title=" + json.data.title);
                    //<div>aaa</div>
                    //html("aaa")
                    $("#product-title").html(json.data.title);
                    $("#product-sell-point").html(json.data.sellPoint);
                    $("#product-price").html(json.data.price);
                    $("#product-num").html(json.data.num);
                    $("#product-priority").html(json.data.priority);
                    $("#product-sell_person").html(json.data.sellPersons);
                    $("#product-judge_mark").html(json.data.judgeMark);

                    for (let i = 1; i <= 5; i++) {
                        $("#product-image-" + i + "-big").attr("src", ".." + json.data.image + i + "_big.png");
                        $("#product-image-" + i).attr("src", ".." + json.data.image + i + ".jpg");
                    }
                } else if (json.state == 4006) { // 商品数据不存在的异常
                    location.href = "index.html";
                } else {
                    alert("获取商品信息失败！" + json.message);
                }
            }
        });

		showProductCommentList();
    });

    $("#btn-add-one-comment").click(function () {
        var jM=judgeMark.value;
        if(jM>10||jM<1){
            alert("请输入1-10的评分！");
            jM.value=null;
        }else{
            $.ajax({
                url:"/comment/insert",
                type:"POST",
                data:$("#form-add-one-comment").serialize(),
                dataType:"JSON",
                success:function (json) {
                    if(json.state==200){
                        alert("感谢评价");
                        console.log(judgeMark);
                        showProductCommentList();
                    }else {
                        alert("评价有误");
                    }
                },
                error:function (xhr) {
                    alert("评价时产生未知的异常"+xhr.message);//自动产生错误描述信息
                }
            })
        }

    })
	function showProductCommentList() {
		$("#comment-list").empty();
		//通过商品id查询历史评价
		$.ajax({
			url: "/comment/find_product",
			type: "GET",
			data:"pid="+id,
			dataType: "JSON",
			success: function(json) {
				let list = json.data;
				console.log("count=" + list.length);
				console.log(json.data);
				for (let i = 0; i < list.length; i++) {
					let tr = '<tr>' +
							'<td>#{uid}</td>' +
							'<td>#{bid}</td>' +
							'<td>#{oid}</td>' +
							'<td>#{pid}</td>'+
							'<td>#{pComment}</td>'+
							'<td>#{bComment}</td>'+
							'<td>#{judgeMark}</td>'+
							'<td>#{reply}</td>'+
							'</tr>'
					tr = tr.replace(/#{uid}/g, list[i].uid);
					tr = tr.replace(/#{bid}/g, list[i].bid);
					tr = tr.replace(/#{oid}/g, list[i].oid);
					tr = tr.replace(/#{pid}/g, list[i].pid);
					tr = tr.replace(/#{pComment}/g, list[i].pcomment);
					tr = tr.replace(/#{bComment}/g, list[i].bcomment);
					tr = tr.replace(/#{judgeMark}/g, list[i].judgeMark);
					tr = tr.replace(/#{reply}/g, list[i].reply);
					$("#comment-list").append(tr);
				}
			}
		});
	}

    $("#btn-add-to-cart").click(function () {
        $.ajax({
            url: "/cart/add_to_cart",
            type: "POST",
            data: {
                "pid": id,
                "amount": $("#num").val()//num按钮的控件
            },
            //ajax函数中data参数的数据设置的方式1.data：$("form表单选择").serialize() 当参数过多并且在同一个表单中，字符串的提交等
            //2.data:new FormData($("form表单选择")[0])只适用提交文件
            //data:"username=AAA",适用于参数值固定且参数值列表有限，可手动拼接
            //JSON格式提交 data:{"username":"ABC"   "age";18   "sex":0}
            dataType: "json",
            success: function (json) {
                if (json.state == 200) {
                    alert("加入购物车成功！");
                } else {
                    alert("加入购物车失败！" + json.message);
                }
            },
            error: function (xhr) {
                alert("请先登录！" + xhr.status);
            }
        });
    });

    $("#btn-search").click(function () {
        $.ajax({
            url: "/product/title",
            type: "POST",
            data: $("#form-search").serialize(),
            dataType: "JSON",
            success: function (json) {
                /*alert(json.state)*/
                if (json.state == 200) {
                    // alert("密码修改成功,请重新登录");
                    //跳转到系统主页
                    //相对路径来确定跳转页面  ./ 上级../
                    location.href = "search.html";

                } else {
                    alert("无此商品！")
                }
            },
            error: function (xhr) {
                alert("搜索商品时产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    })
</script>
</body>

</html>